from django.shortcuts import render
from rest_framework import serializers
from rest_framework.response import Response
from rest_framework.generics import ListAPIView
from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView
from rest_framework_jwt.authentication import jwt_decode_handler

from six_it.serializerss.gatherings import GatheringsModelserializer
from six_it.utils import PageNum
from xyz.models import Gathering
from rest_framework.mixins import RetrieveModelMixin,UpdateModelMixin,CreateModelMixin
# Create your views here.
class GatheringsList(ListAPIView):
    queryset = Gathering.objects.all()
    serializer_class = GatheringsModelserializer
    pagination_class = PageNum
class GatheringsGetView(RetrieveModelMixin,GenericAPIView):
    queryset = Gathering.objects.all()
    serializer_class = GatheringsModelserializer
    lookup_field = 'id'
    def get(self,request,id):
        return self.retrieve(request)
class PostGatheringView(APIView):

    def post(self,request,id):

        token = self.request.auth
        toke_user = jwt_decode_handler(token)
        user_id = toke_user["user_id"]
        cil = Gathering.objects.get(id=id)
        for u in cil.users.all():

            if user_id == u.id:
                cil.users.remove(user_id)
                return Response({
                    'message': '取消报名成功',
                    'success': True
                })
        cil.users.add(user_id)
        return Response({
            'message':'报名成功',
            'success':True
        })